<?php
/**
 * Title:Modle Class Archives
 * Times:2010.04.16
 * Author:雅典娜之子
 */

 !defined('ACCESSING') && exit('Forbidden');

class Archives extends Base
{
	var $arclist;
    var $imglist;
	var $arcinfo;
	var $arcid;
	var $tablename;
	var $baseArray;

	public function __construct()
	{
		parent::__construct();
		$this->tablename = "`".$this->base['prefix']."archives`";
	}

	public function Archives() 
	{
		$this->__construct();
	}

	public function NewsList($cateid='all',$aid=null,$limit='',$if=null)
	{
		$where = "WHERE atype = 'multi'";
		if($cateid != 'all')
		{
			$cateid = intval($cateid);
			$where .= " AND cid IN (" . $cateid . ")";
		}

		if(!empty($aid))
		{
			$aid = intval($aid);
			$where .= " AND aid IN (" . $aid . ")";
		}

        !empty($if) && $where .= " AND ".$if;
		$limit != '' && $limit = "LIMIT ".$limit;

		$sqlconn = "SELECT * FROM {$this->tablename} {$where} ORDER BY `displayorder` DESC,`dateline` DESC {$limit}";
		$result = $this->db->query($sqlconn);
		if($result)
		{
			while($row = $this->db->fetch_array($result))
			{
				$this->arclist[] = $row;
			}

			$nums = $this->db->num_rows($this->db->query("SELECT `aid`,`atitle`,`author`,`cid`,`dateline`,`displayorder` FROM {$this->tablename} {$where}"));
			$arcdata = array($this->arclist,$nums);

			return $arcdata;
		}
		else
		{
			return null;
		}
	}

    public function getfxlist($row='all',$multitable=true,$recommend=false)
    {
        $limit = $where = "";
        $fxdata = array();
        !empty($row) && $limit = "LIMIT ".$row;
        $recfield = "recommend";
        if($multitable === true)
        {
            $recfield = "a.recommend";
            $orderby = " GROUP BY i.attribute ORDER BY a.dateline DESC";
            $where = "a.aid = i.attribute";
        }
        else
        {
            $orderby = " dateline DESC";
            $recfield = "recommend";
            $orderby = " GROUP BY attribute ORDER BY dateline DESC";
        }
        
        if($recommend === true)
        {
            if(!empty($where))
            {
                $where = "WHERE $where AND $recfield = 1";
            }
            else
            {
                $where = "WHERE $recfield = 1";
            }
            $tablename = "jm_archives a,jm_images i";
        }
        else
        {
            $where = "";
            $tablename = "jm_archives";
        }
        $result = $this->db->query("SELECT * FROM $tablename $where $orderby $limit");
        while($row = $this->db->fetch_array($result))
        {
            $fxdata[] = $row;
        }

        return $fxdata;
    }

    public function getfxlist2()
    {
        $fxdata = array();
        $hasfile = FALSE;
        $result = $this->db->query("SELECT * FROM {$this->tablename} WHERE cid = 9 ORDER BY dateline DESC");
        while($row = $this->db->fetch_array($result))
        {
            $xmlfilename = "public/xml/".$row['dateline'].".xml";
            if(!file_exists($xmlfilename))
            {
                $res = $this->db->query("SELECT * FROM `jm_images` WHERE attribute = $row[aid] ORDER BY dateline DESC");
                $nums = $this->db->num_rows($res);
                if($nums > 0)
                {
                    $i=0;
                    $xmlcontent = "<?xml version='1.2' encoding='UTF-8' ?>\n";
                    while($row_img = $this->db->fetch_array($res))
                    {
                        $i++;
                        $xmlcontent .= "<product>\n";
                        $xmlcontent .= "<title value='Product $i' />\n";
                        $xmlcontent .= "<content>$row_img[tname]</content>\n";
                        $xmlcontent .= "<img value='$row_img[tpath]$row_img[tname]' />\n";
                        $xmlcontent .= "<image value='$row_img[tpath]$row_img[tname]' />\n";
                        $xmlcontent .= "</product>\n";
                    }

                    $xmlcontent .= "<option viewImgNum='8' ximg='898' yimg='43' xSpaceimg='3' imgXsize='100' imgYsize='100' maskMargin='3' />\n";
                    $fp = fopen($xmlfilename,"w+");
                    fwrite($fp,$xmlcontent);
                    fclose($fp);
                    $hasfile = TRUE;
                }
            }
            else
            {
                $hasfile = TRUE;
            }
            
            $hasfile == TRUE && $fxdata[] = $row;
        }
        return $fxdata;
    }

	public function imageslist($aid='all',$limit='') 
	{
		$where = "WHERE 1";
		if($aid != 'all')
		{
			$aid = intval($aid);
			$where .= " AND attribute IN (" . $aid . ")";
		}

		$limit != '' && $limit = "LIMIT ".$limit;
		$sqlconn = "SELECT * FROM `jm_images` {$where} ORDER BY `dateline` DESC,`tid` DESC {$limit}";
		$result = $this->db->query($sqlconn);
		if($result)
		{
			while($row = $this->db->fetch_array($result))
			{
				$this->imglist[] = $row;
			}
			$nums = $this->db->num_rows($this->db->query("SELECT `tid`,`attribute` FROM `jm_images` {$where}"));
			$imgdata = array($this->imglist,$nums);

			return $imgdata;
		}
		else
		{
			return null;
		}
	}

	public function GetArcInfo($aid=0) 
	{
		if(is_numeric($aid))
		{
			$sqlconn = "SELECT * FROM {$this->tablename} WHERE aid = {$aid}";
			$this->arcinfo = $this->db->getone($sqlconn);

			if(is_array($this->arcinfo))
			{
				return $this->arcinfo;
			}
			else 
			{
				exit("DATA Error!!");
			}
		}
	}

	public function SetTop($aid=0) 
	{
		if(!is_numeric($aid))
		{
			$aidlist = explode(",",$aid);
			foreach($aidlist as $v)
			{
				if(!is_numeric(intval($v)))
				{
					exit("archives id error!!");
				}
			}
		}
		
		$sqlconn = "UPDATE {$this->tablename} SET displaystate = 1 WHERE aid IN ({$aid})";
		$result = $this->db->query($sqlconn);

		if(!$result)
		{
			exit("SQL Error!!");
		}
		else 
		{
			return true;
		}
	}

	public function InsertData($arcdata) 
	{
		if(is_array($arcdata))
		{
			$result = $this->db->query("INSERT INTO {$this->tablename}(atitle,cid,author,price,archivespic,displaystate,displayorder,starttime,endtime,dateline,intro,installation,recommend) VALUES('".$arcdata['ntitle']."',".$arcdata['category'].",'".GetSession('UserName')."','".$arcdata['price']."','".$arcdata['arcpic']."',0,0,".$arcdata['starttime'].",".$arcdata['endtime'].",".$arcdata['dateline'].",'".$arcdata['intro']."','".$arcdata['installation']."',".$arcdata['recommend'].")");
			if(!$result)
			{
				exit("SQL Error!!");
			}
			else 
			{
				return true;
			}
		}
		else
		{
			exit("DATA Error!!");
		}
	}

	public function Update($aid,$arcdata,$arctype='single')
	{
		!is_array($arcdata) && exit("DATA Error!!");

		if($arctype == 'single')
		{
			$sqlconn = "UPDATE {$this->tablename} SET installation = '".$arcdata['content']."' WHERE aid = {$arcdata['aid']}";
		}
		elseif ($arctype == 'multi') 
		{
			$sqlconn = "UPDATE {$this->tablename} SET
			atitle = '{$arcdata['ntitle']}',
			cid = {$arcdata['category']},
			price = '{$arcdata['price']}',
			archivespic = '{$arcdata['arcpic']}',
            recommend = '{$arcdata['recommend']}',
			starttime = {$arcdata['starttime']},
			endtime = {$arcdata['endtime']},
			intro = '{$arcdata['intro']}',
			installation = '{$arcdata['installation']}'  WHERE aid = {$arcdata['aid']}";
		}

		$result = $this->db->query($sqlconn);

		if(!$result)
		{
			exit("Query Error!!");
		}
		else 
		{
			return true;
		}
	}

	public function Del($aids) 
	{
		$aid = "";
		if(!is_numeric($aids) && !is_array($aids))
		{
			$aidlist = explode(",",$aids);
			foreach($aidlist as $v)
			{
				if(!is_numeric($v))
				{
					return "archives id error!!";
				}
			}
			$aid = $aids;
		}

		if(is_array($aids))
		{
			foreach($aids as $v)
			{
				if(!is_numeric($v))
				{
					return "archives id error!!";
				}
				$aid .= $v.",";
			}

			$aid = substr($aid,0,-1);
		}

		$result = $this->db->query("DELETE FROM {$this->tablename} WHERE aid IN (".$aid.")");
		if($result)
		{
			return true;
		}
	}

    public function del_img($tids)
    {
        $tid = "";
        $tidlist = explode(",",$tids);
        foreach($tidlist as $v)
        {
            if(!is_numeric($v))
            {
                return "archives id error!!";
            }
        }

		if(is_array($tidlist))
		{
			foreach($tidlist as $v)
			{
				if(!is_numeric($v))
				{
					return "images id error!!";
				}

                $tprow = $this->db->getone("SELECT `tid`,`tname`,`tpath` FROM `jm_images` WHERE `tid` = $v");
                if(is_array($tprow))
                {
                    $tp = D_R.$tprow['tpath'].$tprow['tname'];
                    if(file_exists($tp))
                    {
                        unlink($tp);
                    }
                }
				$tid .= $v.",";
			}

			$tid = substr($tid,0,-1);
		}

		$result = $this->db->query("DELETE FROM `jm_images` WHERE tid IN (".$tid.")");
		if($result)
		{
			return true;
		}
    }

	public function GetCateName($cid) 
	{
		if(is_numeric($cid))
		{
			$result = $this->db->getone("SELECT cid,cname FROM jm_category WHERE cid = ".$cid."");
			if(!$result)
				return null;
			else
				return $result;
		}
	}

	public function Search($key) 
	{
		$sqlconn = "SELECT `aid`,`atitle`,`author`,`cid`,`dateline`,`displayorder` FROM {$this->tablename} WHERE atitle LIKE '%{$key}%' AND atype = 'multi' ORDER BY `dateline` DESC {$limit}";
		$result = $this->db->query($sqlconn);
		if(!empty($result))
		{
			while($row = $this->db->fetch_array($result))
			{
				$arcdata[] = $row;
			}
			return $arcdata;
		}
	}

	public function getfxtitle() 
	{
		$sqlconn = "SELECT `aid`,`cid`,`atitle` FROM {$this->tablename} WHERE cid = 9 AND atype = 'multi' ORDER BY `dateline` DESC";
		$result = $this->db->query($sqlconn);
		if(!empty($result))
		{
			while($row = $this->db->fetch_array($result))
			{
				$arcdata[] = $row;
			}
			return $arcdata;
		}
	}
}
?>